package com.qike.yuandongguoji.controller;

import com.qike.yuandongguoji.dto.*;
import com.qike.yuandongguoji.pojo.UseCar;
import com.qike.yuandongguoji.response.BaseResult;
import com.qike.yuandongguoji.response.RetResponse;
import com.qike.yuandongguoji.service.PcService;
import com.qike.yuandongguoji.util.ExcleUtil;
import com.qike.yuandongguoji.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * @Author: Eric.zhou
 * @Date: 2020/8/5 10:38
 */
@RestController
@Api("pc端")
@RequestMapping("/pc")
@CrossOrigin
public class PcController {


    @Autowired
    private PcService pcService;

    @ApiOperation("登录")
    @PostMapping("/login")
    @ResponseBody

    public BaseResult PCLogin(@RequestBody PcLoginDto loginDto) throws URISyntaxException, IOException {
        try {
            PCLoginVo loginVo = pcService.login(loginDto);
            return RetResponse.makeOKRsp(loginVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("请假统计列表")
    @GetMapping("/leaveList")
    @ResponseBody

    public BaseResult leaveList(LeaveListDto loginDto) throws URISyntaxException, IOException {
        try {
            LeaveListVo loginVo = pcService.leaveList(loginDto);
            return RetResponse.makeOKRsp(loginVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("请假统计列表下载")
    @GetMapping("/leaveListDownLoad")
    public BaseResult leaveListDownLoad(LeaveListDto loginDto, HttpServletResponse httpServletResponse) throws URISyntaxException, IOException {
        try {
            List<LeaveListDownLoadVo> list = pcService.leaveListDownLoad(loginDto);
            ExcleUtil.exportExcel(list, "请假统计", "sheet1", LeaveListDownLoadVo.class, "请假统计报表" + ".xls", httpServletResponse);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 上传文件
     *
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/upLoadPhoto")
    @ResponseBody
//    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult upLoadPhoto(@RequestBody MultipartFile file) {
        try {
            String url = pcService.upLoadPhoto(file);
            return RetResponse.makeOKRsp(url);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("请假统计批量修改状态")
    @PostMapping("/leaveChangeStatus")
    @ResponseBody
    public BaseResult leaveChangeStatus(@RequestBody LeaveChangeStatusDto leaveChangeStatusDto) throws URISyntaxException, IOException {
        try {
            pcService.leaveChangeStatus(leaveChangeStatusDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("差旅行程列表")
    @GetMapping("/outList")
    @ResponseBody

    public BaseResult outList(OutListDto outListDto) throws URISyntaxException, IOException {
        try {
            OutListVo outList = pcService.outList(outListDto);
            return RetResponse.makeOKRsp(outList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("新差旅行程列表")
    @GetMapping("/newOutList")
    @ResponseBody

    public BaseResult newOutList(NewOutListDto newOutListDto) throws URISyntaxException, IOException {
        try {
            List<NewOutListVo> listVos = pcService.newOutList(newOutListDto);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("查看一个人全部行程")
    @GetMapping("/userNewOutList")
    @ResponseBody
    public BaseResult userNewOutList(UserNewOutListDto userNewOutListDto) throws URISyntaxException, IOException {
        try {
            List<NewOutListVo> listVos = pcService.userNewOutList(userNewOutListDto);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("添加差旅")
    @PostMapping("/outAdd")
    @ResponseBody
    public BaseResult outAdd(@RequestBody OutAddtDto outAddtDto) throws URISyntaxException, IOException {
        try {
            pcService.outAdd(outAddtDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

//    public static void main(String[] args) {
//        String projectName = "天水市有轨电车项目渭河麦积城区段北岸一泵两闸迁建工程";
//        if (projectName.length()>10){
//            projectName = projectName.substring(0,10);
//        }
//        System.out.println(projectName);
//    }

    @ApiOperation("删除差旅")
    @DeleteMapping("/outDel")
    @ResponseBody
    public BaseResult outDel(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.outDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("假期列表")
    @GetMapping("/leaveTypeList")
    @ResponseBody
    public BaseResult leaveTypeList() throws URISyntaxException, IOException {
        try {
            List<LeaveTypeListVo> listVos = pcService.leaveTypeList();
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("差旅行程详情")
    @GetMapping("/outInfo")
    @ResponseBody
    public BaseResult outInfo(Integer id) throws URISyntaxException, IOException {
        try {
            OutInfoVo outList = pcService.outInfo(id);
            return RetResponse.makeOKRsp(outList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("差旅行程批量修改状态")
    @PostMapping("/outChangeStatus")
    @ResponseBody
    public BaseResult outChangeStatus(@RequestBody LeaveChangeStatusDto leaveChangeStatusDto) throws URISyntaxException, IOException {
        try {
            pcService.outChangeStatus(leaveChangeStatusDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("用车申请列表")
    @GetMapping("/useCarList")
    @ResponseBody
    public BaseResult useCarList(UseCarListDto useCarListDto) throws URISyntaxException, IOException {
        try {
            UserCarVo userCarVo = pcService.useCarList(useCarListDto);
            return RetResponse.makeOKRsp(userCarVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("新用车申请列表")
    @GetMapping("/newUseCarList")
    @ResponseBody
    public BaseResult newUseCarList(String dates) throws URISyntaxException, IOException {
        try {
//            UserCarVo userCarVo = pcService.newUseCarList(useCarListDto);
            List<NewUseCarListVo> list = pcService.newUseCarList(dates);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("用车申请列表下载")
    @GetMapping("/useCarListDownLoad")
    @ResponseBody
    public BaseResult useCarListDownLoad(UseCarListDto useCarListDto, HttpServletResponse httpServletResponse) throws URISyntaxException, IOException {
        try {
            List<UseCarListDownLoadVo> list = pcService.useCarListDownLoad(useCarListDto);
            ExcleUtil.exportExcel(list, "用车申请", "sheet1", UseCarListDownLoadVo.class, "用车申请统计报表" + ".xls", httpServletResponse);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("确认用车申请")
    @PostMapping("/carConfirm")
    @ResponseBody
    public BaseResult carConfirm(@RequestBody CarConfirmDto carConfirmDto) throws URISyntaxException, IOException {
        try {
            pcService.carConfirm(carConfirmDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("拒绝用车申请")
    @PostMapping("/carRefuse")
    @ResponseBody
    public BaseResult carRefuse(@RequestBody CarRefuseDto carRefuseDto) throws URISyntaxException, IOException {
        try {
            pcService.carRefuse(carRefuseDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("PC订车")
    @PostMapping("/PCOrderCar")
    @ResponseBody
    public BaseResult PCOrderCar(@RequestBody PCOrderCarDto pcOrderCarDto) throws URISyntaxException, IOException {
        try {
            pcService.PCOrderCar(pcOrderCarDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("PC订车取消")
    @PostMapping("/PCOrderCarDel")
    @ResponseBody
    public BaseResult PCOrderCarDel(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.PCOrderCarDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("PC订车编辑")
    @PostMapping("/PCOrderCarEdit")
    @ResponseBody
    public BaseResult PCOrderCarEdit(@RequestBody PCOrderCarEditDto pcOrderCarEditDto) throws URISyntaxException, IOException {
        try {
            pcService.PCOrderCarEdit(pcOrderCarEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("PC订车详情")
    @GetMapping("/PCOrderCarInfo")
    @ResponseBody
    public BaseResult PCOrderCarInfo(Integer id) throws URISyntaxException, IOException {
        try {
            UseCar vo = pcService.PCOrderCarInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("会议室日历  参数格式 2020-08")
    @GetMapping("/meetingList")
    @ResponseBody
    public BaseResult meetingList(String dates) throws URISyntaxException, IOException {
        try {
            List<MeetingListVo> list = pcService.meetingList(dates);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("取消会议")
    @PostMapping("/cancelMeeting")
    @ResponseBody
    public BaseResult cancelMeeting(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.cancelMeeting(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("预订会议")
    @PostMapping("/orderMeeting")
    @ResponseBody
    public BaseResult orderMeeting(@RequestBody OrderMeetingDto orderMeetingDto) throws URISyntaxException, IOException {
        try {
            pcService.orderMeeting(orderMeetingDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("员工列表")
    @GetMapping("/userList")
    @ResponseBody
    public BaseResult userList(UserListDto userListDto) throws URISyntaxException, IOException {
        try {
            UserListVo vo = pcService.userList(userListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("根据部门返回员工列表")
    @GetMapping("/userListByDeptId")
    @ResponseBody
    public BaseResult userListByDeptId(Integer deptId) throws URISyntaxException, IOException {
        try {
            List<UserListByDeptIdVo> vo = pcService.userListByDeptId(deptId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("添加员工")
    @PostMapping("/addUser")
    @ResponseBody
    public BaseResult addUser(@RequestBody AddUserDto addUserDto) throws URISyntaxException, IOException {
        try {
            pcService.addUser(addUserDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("编辑员工")
    @PostMapping("/editUser")
    @ResponseBody
    public BaseResult editUser(@RequestBody EditUserDto editUserDto) throws URISyntaxException, IOException {
        try {
            pcService.editUser(editUserDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("删除员工")
    @DeleteMapping("/delUser")
    @ResponseBody
    public BaseResult delUser(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.delUser(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("部门列表")
    @GetMapping("/deptList")
    @ResponseBody
    public BaseResult deptList(DeptListDto deptListDto) throws URISyntaxException, IOException {
        try {
            DeptListVo vo = pcService.deptList(deptListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("部门下拉列表")
    @GetMapping("/allDeptList")
    @ResponseBody
    public BaseResult allDeptList(String name) throws URISyntaxException, IOException {
        try {
            List<AllDeptListVo> vo = pcService.allDeptList(name);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("部门编辑")
    @PostMapping("/deptEdit")
    @ResponseBody
    public BaseResult deptEdit(DeptEditDto deptEditDto) throws URISyntaxException, IOException {
        try {
            pcService.deptEdit(deptEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("添加部门")
    @PostMapping("/deptAdd")
    @ResponseBody
    public BaseResult deptAdd(DeptAddDto deptAddDto) throws URISyntaxException, IOException {
        try {
            pcService.deptAdd(deptAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/deptDel")
    @ResponseBody
    public BaseResult deptDel(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.deptDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("车辆列表")
    @GetMapping("/carList")
    @ResponseBody
    public BaseResult carList(CarListDto carListDto) throws URISyntaxException, IOException {
        try {
            CarListVo vo = pcService.carList(carListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("添加车辆")
    @PostMapping("/carAdd")
    @ResponseBody
    public BaseResult carAdd(CarAddDto carAddDto) throws URISyntaxException, IOException {
        try {
            pcService.carAdd(carAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("车辆编辑")
    @PostMapping("/carEdit")
    @ResponseBody
    public BaseResult carEdit(@RequestBody CarEditDto carEditDto) throws URISyntaxException, IOException {
        try {
            pcService.carEdit(carEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("车辆删除")
    @DeleteMapping("/carDel")
    @ResponseBody
    public BaseResult carDel(Integer id) throws URISyntaxException, IOException {
        try {
            pcService.carDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("用户下拉列表")
    @GetMapping("/userAllList")
    @ResponseBody
    public BaseResult userAllList(String name, String role) throws URISyntaxException, IOException {
        try {
            List<UserAllListVo> listVos = pcService.userAllList(name, role);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("职位列表")
    @GetMapping("/positionList")
    @ResponseBody
    public BaseResult positionList() throws URISyntaxException, IOException {
        try {
            List<PositionListVo> stringMap = pcService.positionList();
            return RetResponse.makeOKRsp(stringMap);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("车辆下拉列表")
    @GetMapping("/carNOList")
    @ResponseBody
    public BaseResult carNoList(String carNo) {
        try {
            List<CarNoListVo> listVos = pcService.carNoList(carNo);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("通知列表")
    @GetMapping("/newsList")
    @ResponseBody
    public BaseResult newsList(NewsListDto newsListDto) {
        try {
            NewsListVo listVos = pcService.newsList(newsListDto);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("获取通知总数")
    @GetMapping("/newsCount")
    @ResponseBody
    public BaseResult newsCount() {
        try {
            int count = pcService.newsCount();
            return RetResponse.makeOKRsp(count);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("添加通知")
    @PostMapping("/addNews")
    @ResponseBody
    public BaseResult addNews(@RequestBody AddNews addNews) {
        try {
            pcService.addNews(addNews);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
